﻿using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Threading;

namespace VisionApp.Startup;

public partial class SplashScreen : Window
{
    private DispatcherTimer _progressTimer;
    private double _progress = 0;
    public event EventHandler? SplashCompleted;

    private readonly string[] _progressTexts = new string[]
    {
        "初始化系统...",
        "加载驱动程序...",
        "检测硬件配置...",
        "启动AI引擎...",
        "建立数据连接...",
        "优化性能参数...",
        "完成系统检查..."
    };

    private readonly string[] _statusMessages = new string[]
    {
        "正在加载核心组件...",
        "校准传感器参数...",
        "初始化图像处理模块...",
        "加载深度学习模型...",
        "建立安全连接...",
        "优化视觉算法...",
        "准备用户界面..."
    };

    public SplashScreen()
    {
        InitializeComponent();

        // 启动浮动动画
        //Storyboard floatAnimation = (Storyboard)FindResource("FloatAnimation");
        //Storyboard.SetTarget(floatAnimation, FloatWindow);
        //floatAnimation.Begin();

        // 启动进度更新计时器
        
        int interval = 100;

        if (Debugger.IsAttached) interval = 1;

        _progressTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(interval) };
        _progressTimer.Tick += ProgressTimer_Tick;
        _progressTimer.Start();
    }
    private void ProgressTimer_Tick(object sender, EventArgs e)
    {
        // 随机增加进度，但最后阶段增加速度放缓
        double increment = _progress < 80 ? new Random().NextDouble() * 3 + 1 : new Random().NextDouble() * 1 + 0.5;
        _progress += increment;

        if (_progress >= 100)
        {
            _progress = 100;
            _progressTimer.Stop();

            // 完成后的状态更新
            ProgressText.Text = "启动完成";
            StatusMessage.Text = "系统就绪，正在进入...";

            // 延迟后淡出
            DispatcherTimer closeTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(1.5) };
            closeTimer.Tick += (s, args) => {
                closeTimer.Stop();
                if (FindResource("FadeOutAnimation") is Storyboard fadeOutAnimation)
                {
                    fadeOutAnimation.Completed += (_, _) =>
                    {
                        SplashCompleted?.Invoke(this, EventArgs.Empty);
                        this.Close();
                    };
                    Storyboard.SetTarget(fadeOutAnimation, this);
                    fadeOutAnimation.Begin();
                }
                else
                {
                    SplashCompleted?.Invoke(this, EventArgs.Empty);
                    this.Close();
                }

            };
            closeTimer.Start();
        }

        // 更新UI
        ProgressBar.Width = 640 * (_progress / 100);
        ProgressPercentage.Text = $"{Math.Round(_progress)}%";

        // 根据进度更新状态文本
        int textIndex = Math.Min((int)Math.Floor(_progress / 15), _progressTexts.Length - 1);
        ProgressText.Text = _progressTexts[textIndex];

        int messageIndex = Math.Min((int)Math.Floor(_progress / 14), _statusMessages.Length - 1);
        StatusMessage.Text = _statusMessages[messageIndex];
    }

    private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // 允许拖动窗口
        DragMove();
    }

    private void MinimizeButton_Click(object sender, RoutedEventArgs e)
    {
        // 最小化窗口
        WindowState = WindowState.Minimized;
    }

    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        // 关闭应用程序
        //Application.Current.Shutdown();
    }
}